<?php

date_default_timezone_set('Asia/Saigon');

class ShopManagerController extends Controller {

    public $layout = '//layouts/manager';

    public function actionIndex() {
        //setupshop
    }

    public function actionDynamicMenushop() {
        $data = ShopMenu::model()->findAll('parentID=0 and shopid=:shopid', array(':shopid' => (int) $_POST['id']));
        $value0 = 0;
        $data = CHtml::listData($data, 'id', 'title');
        echo CHtml::tag('option', array('value' => $value0), '----- Chọn menu cha -----', true);
        foreach ($data as $value => $name) {
            echo CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
        }
        /*
          if (!empty($data)) {
          foreach ($data as $item) {
          echo CHtml::tag('option', array('value' => $item->disID), $item->disName, true);
          }
         */
    }

    public function actionDynamicCategory() {
        $data = ShopNavbar::model()->findAll('parent=0 and shopid=:shopid', array(':shopid' => (int) $_POST['id']));
        $value0 = 0;
        $data = CHtml::listData($data, 'id', 'title');
        echo CHtml::tag('option', array('value' => $value0), '----- Chọn danh mục cha -----', true);
        foreach ($data as $value => $name) {
            echo CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
        }
        /*
          if (!empty($data)) {
          foreach ($data as $item) {
          echo CHtml::tag('option', array('value' => $item->disID), $item->disName, true);
          }
         */
    }

    public function actionDynamicProduct() {
        $data = ShopNavbar::model()->findAll('shopid=:shopid', array(':shopid' => (int) $_POST['id']));
        foreach ($data as $item) {
            if ($item->parent > 0) {
                $item->title = '   + ' . $item->title;
            }
        }
        $value0 = 0;
        $data = CHtml::listData($data, 'id', 'title');
        //echo CHtml::tag('option', array('value' => $value0), '----- Chọn danh mục cha -----', true);
        foreach ($data as $value => $name) {
            echo CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
        }
    }

    public function actionDynamicCity() {
        $data = ShopCity::model()->findAll('flag=1 and shopid=:shopid', array(':shopid' => (int) $_POST['id']));
        // $value0 = 0;
        $data = CHtml::listData($data, 'id', 'cityName');
        //echo CHtml::tag('option', array('value' => $value0), '----- Chọn danh mục cha -----', true);
        foreach ($data as $value => $name) {
            echo CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
        }
    }

    public function actionList_Shop() {
        $criteria = new CDbCriteria();
        $session = new CHttpSession;
        $session->open();
        if (!isset($session['user_salesmart'])) {
            $this->redirect('http://www.salesmartlink.com/');
        }
        $userID = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] < 99) {
            $criteria->condition = ' flag in(1,4) and uid =' . $userID;
        } else {
            $criteria->condition = ' flag in(1,2,4)';
        }
        $shop = Shop::model()->findAll($criteria);
        $this->render('///manager/ca-nhan', array(
            'shop' => $shop,));
    }

    public function actionAddShop() {

        $model = new Shop();
        $rnd = rand(0, 99999);
        $this->pageTitle = 'Tạo Gian Hàng';
        if (isset($_POST['Shop'])) {

            $model->attributes = $_POST['Shop'];
            $uploadedFile = CUploadedFile::getInstance($model, 'logo');
            $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
            if (!empty($uploadedFile)) {
                $model->logo = $fileName;
            }
            $model->datecreate = new CDbExpression('NOW()');

            $session = new CHttpSession;
            $session->open();

            $model->uid = $session['user_salesmart'][3];
            //if ($session['user_salesmart'][1] < 99) {
            // $model->flag = 0;
            // }

            if ($model->save()) {
                $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/icon/' . $model->logo);
                $url = Yii::app()->createUrl('danh-sach-shop');
                $this->redirect($url);
            }
        }

        $this->render('///manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdateShop($id) {
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_shop($md->decrypt($id));
        $rnd = rand(0, 99999);
        $oldphoto = $model->logo;
        if (isset($_POST['Shop'])) {
            $model->attributes = $_POST['Shop'];
            $uploadedFile = CUploadedFile::getInstance($model, 'logo');
            $model->logo = $oldphoto;
            $up = 0;
            if (!empty($uploadedFile)) {
                $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                $up = 1;
                $model->logo = $fileName;
            }
            if ($model->save()) {
                if ($up > 0) {
                    $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/icon/' . $model->logo);
                }
                $url = Yii::app()->createUrl('danh-sach-shop');
                $this->redirect($url);
            }
        }

        $this->render('///manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeleteShop($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_shop($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();
        $model->uid = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-shop');
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-shop');
        $this->redirect($url);
        $this->render('//manager/ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

 

 
   
    public function actionListPageshop() {
        $this->pageTitle = 'Quản lý bài viết';
        $this->render('//manager/ca-nhan', array(
        ));
    }

    public function actionDsPageshop($id) {

        $session = new CHttpSession;
        $session->open();

        $model = ShopPage::model()->findAll('flag in(0,1) and shopid=' . $id . ' and uid=' . $session['user_salesmart'][3]);

        $this->pageTitle = 'Danh sách bài viét';
        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionAddPageshop() {

        $model = new ShopPage();

        $this->pageTitle = 'Tạo Page';
        if (isset($_POST['ShopPage'])) {

            $model->attributes = $_POST['ShopPage'];


            //$model->dateCreate = new CDbExpression('NOW()');
            //$model->money = 0;
            $session = new CHttpSession;
            $session->open();

            $model->uid = $session['user_salesmart'][3];


            if ($model->save()) {

                $url = Yii::app()->createUrl('danh-sach-page-shop/' . $model->shopid);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdatePageshop($id) {
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_pageshop($md->decrypt($id));
        if (isset($_POST['ShopPage'])) {
            $model->attributes = $_POST['ShopPage'];
            if ($model->save()) {

                $url = Yii::app()->createUrl('danh-sach-page-shop/' . $model->shopid);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeletePageshop($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_pageshop($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();
        $model->uid = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-page-shop/' . $model->shopid);
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-page-shop/' . $model->shopid);
        $this->redirect($url);
        $this->render('//manager/ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionAddCity() {

        $model = new ShopCity();

        $this->pageTitle = 'Thêm tỉnh/thành';
        if (isset($_POST['ShopCity'])) {

            $model->attributes = $_POST['ShopCity'];
            $session = new CHttpSession;
            $session->open();
            $model->shopid = $session['user_salesmart'][3];

            if ($model->save()) {

                $url = Yii::app()->createUrl('danh-sach-city');
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdateCity($id) {
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_Cityshop($md->decrypt($id));
        if (isset($_POST['ShopCity'])) {
            $model->attributes = $_POST['ShopCity'];
            if ($model->save()) {

                $url = Yii::app()->createUrl('danh-sach-city');
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeleteCity($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_Cityshop($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();

        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-city');
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-city');
        $this->redirect($url);
        $this->render('//manager/ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionListMenu() {
        $this->pageTitle = 'Quản lý menu';
        $this->render('//manager/ca-nhan', array(
        ));
    }

    public function actionDsMenu($id) {

        $session = new CHttpSession;
        $session->open();

        $model = ShopMenu::model()->findAll('flag in(0,1) and shopid=' . $id . ' and uid=' . $session['user_salesmart'][3]);

        $this->pageTitle = 'Thêm menu';
        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionAddMenu() {

        $model = new ShopMenu();

        $this->pageTitle = 'Thêm menu';
        if (isset($_POST['ShopMenu'])) {

            $model->attributes = $_POST['ShopMenu'];
            $session = new CHttpSession;
            $session->open();
            $model->uid = $session['user_salesmart'][3];
            if ($model->parentID == NULL) {
                $model->parentID = 0;
            }

            if ($model->save()) {

                $url = Yii::app()->createUrl('gian-hang-menu');
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdateMenu($id) {
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_Menushop($md->decrypt($id));
        if (isset($_POST['ShopMenu'])) {
            $model->attributes = $_POST['ShopMenu'];
            if ($model->save()) {

                $url = Yii::app()->createUrl('gian-hang-menu');
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeleteMenu($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_Menushop($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();

        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('gian-hang-menu');
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('gian-hang-menu');
        $this->redirect($url);
        $this->render('//manager/ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionListCategory() {
        $this->pageTitle = 'Quản lý danh mục';
        $this->render('//manager/ca-nhan', array(
        ));
    }

    public function actionDsCategory($id) {

        $session = new CHttpSession;
        $session->open();

        $model = ShopNavbar::model()->findAll('flag in(0,1) and shopid=' . $id . ' and uid=' . $session['user_salesmart'][3]);

        $this->pageTitle = 'Thêm danh mục';
        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionAddCategory() {

        $model = new ShopNavbar();
        $rnd = rand(0, 99999);
        $this->pageTitle = 'Thêm danh mục';
        if (isset($_POST['ShopNavbar'])) {

            $model->attributes = $_POST['ShopNavbar'];
            $uploadedFile = CUploadedFile::getInstance($model, 'image');
            $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
            if (!empty($uploadedFile)) {
                $model->image = $fileName;
            }


            $session = new CHttpSession;
            $session->open();

            $model->uid = $session['user_salesmart'][3];
            //if ($session['user_salesmart'][1] < 99) {
            // $model->flag = 0;
            // }

            if ($model->save()) {
                $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/icon/' . $model->image);
                $url = Yii::app()->createUrl('danh-sach-danh-muc/' . $model->shopid);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdateCategory($id) {
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_shopCategory($md->decrypt($id));
        $rnd = rand(0, 99999);
        $oldphoto = $model->image;
        if (isset($_POST['ShopNavbar'])) {
            $model->attributes = $_POST['ShopNavbar'];
            $uploadedFile = CUploadedFile::getInstance($model, 'image');
            $model->image = $oldphoto;
            $up = 0;
            if (!empty($uploadedFile)) {
                $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                $up = 1;
                $model->image = $fileName;
            }
            if ($model->save()) {
                if ($up > 0) {
                    $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/icon/' . $model->image);
                }
                $url = Yii::app()->createUrl('danh-sach-danh-muc/' . $model->shopid);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeleteCategory($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_shopCategory($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();

        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-danh-muc/' . $model->shopid);
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-danh-muc/' . $model->shopid);
        $this->redirect($url);
        $this->render('//manager/ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionListSlider() {
        $this->pageTitle = 'Quản lý danh mục';
        $this->render('//manager/ca-nhan', array(
        ));
    }

    public function actionDsSlider($id) {

        $session = new CHttpSession;
        $session->open();

        $model = ShopSlider::model()->findAll('flag in(0,1) and shopid=' . $id . ' and uid=' . $session['user_salesmart'][3]);

        $this->pageTitle = 'Thêm danh mục';
        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionAddSliderShop() {

        $model = new ShopSlider;
        $rnd = rand(0, 99999);
        $ketqua = 0;
        $this->pageTitle = 'Tạo slider';
        if (isset($_POST['ShopSlider'])) {

            $model->attributes = $_POST['ShopSlider'];
            $uploadedFile = CUploadedFile::getInstance($model, 'photo');
            $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
            if (!empty($uploadedFile)) {
                $model->photo = $fileName;
            }

            $model->dateCreate = new CDbExpression('NOW()');
            $model->money = 0;
            $session = new CHttpSession;
            $session->open();

            $model->uid = $session['user_salesmart'][3];

            if ($model->save()) {
                $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/slider/' . $model->photo);

                $ketqua = 1;

                $url = Yii::app()->createUrl('danh-sach-slider-shop/' . $model->shopid);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdateSliderShop($id) {
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_slidershop($md->decrypt($id));

        $rnd = rand(0, 99999);
        $oldphoto = $model->photo;
        $up = 0;
        if (isset($_POST['ShopSlider'])) {
            $model->attributes = $_POST['ShopSlider'];
            $uploadedFile = CUploadedFile::getInstance($model, 'photo');
            $model->photo = $oldphoto;
            if (!empty($uploadedFile)) {
                $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                $up = 1;
                $model->photo = $fileName;
            }

            if ($model->save()) {
                if ($up > 0) {
                    $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/upload/product/slider/' . $model->photo);
                }
                $url = Yii::app()->createUrl('danh-sach-slider-shop/' . $model->shopid);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeleteSliderShop($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_slidershop($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();
        $model->uid = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-slider-shop/' . $model->shopid);
            $this->redirect($url);
        }
        $url = Yii::app()->createUrl('danh-sach-slider-shop/' . $model->shopid);
        $this->redirect($url);
        $this->render('//manager/ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionListpanelshop() {
        $this->pageTitle = 'Quản lý quảng cáo';
        $this->render('//manager/ca-nhan', array(
        ));
    }
    public function actionDsPanelshop($id) {
        $criteria = new CDbCriteria();
        $session = new CHttpSession;
        $session->open();

        $userID = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] < 99) {
            $criteria->condition = ' flag<2 and uid =' . $userID .' and shopid='.$id;
        } else {
            $criteria->condition = ' flag <3 and shopid='.$id;
        }
        $model = ShopPanel::model()->findAll($criteria);
        $this->render('//manager/ca-nhan', array(
            'model' => $model,));
    }
    
    public function actionAddPanelshop() {

        $model = new ShopPanel();
        $rnd = rand(0, 99999);
        $ketqua = 0;
        $this->pageTitle = 'Tạo panel';
        if (isset($_POST['ShopPanel'])) {

            $model->attributes = $_POST['ShopPanel'];
            $uploaded = CUploadedFile::getInstance($model, 'photo');
            $fileName = "{$rnd}-{$uploaded}";  // random number + file name
            if (!empty($uploaded)) {
                $model->photo = $fileName;
            }

            $session = new CHttpSession;
            $session->open();

            $model->uid = $session['user_salesmart'][3];
           
      
            if ($model->save()) {
                $uploaded->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/quangcao/' . $model->photo);
                $ketqua = 1;
                $url = Yii::app()->createUrl('danh-sach-banner-shop/'.$model->id);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdatePanelshop($id) {
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_panelshop($md->decrypt($id));

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        $rnd = rand(0, 99999);
        $oldphoto = $model->photo;
        $up = 0;
        if (isset($_POST['ShopPanel'])) {
            $model->attributes = $_POST['ShopPanel'];
            $uploadedFile = CUploadedFile::getInstance($model, 'photo');
            $model->photo = $oldphoto;
            if (!empty($uploadedFile)) {
                $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                $up = 1;
                $model->photo = $fileName;
            }

            if ($model->save()) {
                if ($up > 0) {
                    $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/quangcao/' . $model->photo);
                }
                $url = Yii::app()->createUrl('danh-sach-banner-shop/'.$model->id);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeletePanelshop($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_panelshop($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();
        $model->uid = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-banner-shop/'.$model->id);
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-banner-shop/'.$model->id);
        $this->redirect($url);
        $this->render('//manager/ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionListProductshop() {
        $this->pageTitle = 'Quản lý sản phẩm';
        $this->render('//manager/ca-nhan', array(
        ));
    }

    public function actionDsProductshop($id) {

        $criteria = new CDbCriteria();
        $session = new CHttpSession;
        $session->open();

        $userID = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] < 99) {
            $criteria->condition = ' flag<2 and uid =' . $userID . ' and shopid=' . $id;
        } else {
            $criteria->condition = ' flag <3 and shopid=' . $id;
        }
        $model = ShopProduct::model()->findAll($criteria);

        $this->pageTitle = 'Danh sách sản phẩm';
        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionAddProductshop() {

        $model = new ShopProduct();
        $rnd = rand(0, 99999);
        $ketqua = 0;
        $this->pageTitle = 'Tạo sản phẩm';
        if (isset($_POST['ShopProduct'])) {

            $model->attributes = $_POST['ShopProduct'];
            $uploaded = CUploadedFile::getInstance($model, 'avatar');
            $fileName = "{$rnd}-{$uploaded}";  // random number + file name
            if (!empty($uploaded)) {
                $model->avatar = $fileName;
            }

            $model->pr_datecreate = new CDbExpression('NOW()');
            $model->pr_dateup = new CDbExpression('NOW()');
            $model->pr_stardate=new CDbExpression('NOW()');
            //$model->dateCreate = new CDbExpression('NOW()');
            //$model->money = 0;
            $session = new CHttpSession;
            $session->open();

            $model->uid = $session['user_salesmart'][3];
            if ($session['user_salesmart'][1] < 98) {
                $model->flag = 0;
            }

            if ($model->save()) {
                $uploaded->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/product/' . $model->avatar);
                $ketqua = 1;
                $url = Yii::app()->createUrl('danh-sach-product-shop/' . $model->shopid);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdateProductshop($id) {
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_productshop($md->decrypt($id));

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        $rnd = rand(0, 99999);
        $oldphoto = $model->avatar;
        $up = 0;
        if (isset($_POST['ShopProduct'])) {
            $model->attributes = $_POST['ShopProduct'];
            $uploadedFile = CUploadedFile::getInstance($model, 'avatar');
            $model->avatar = $oldphoto;
            $session = new CHttpSession;
            $session->open();

            $model->uid = $session['user_salesmart'][3];
            if (!empty($uploadedFile)) {
                $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                $up = 1;
                $model->avatar = $fileName;
            }

            if ($model->save()) {
                if ($up > 0) {
                    $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/product/' . $model->avatar);
                }
                $url = Yii::app()->createUrl('danh-sach-product-shop/' . $model->shopid);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeleteProductshop($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.home.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_productshop($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();
        $model->uid = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-product-shop/' . $model->shopid);
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-product-shop/' . $model->shopid);
        $this->redirect($url);
        $this->render('//manager/ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

   
    public function loadModel_panelshop($id) {
        $model = ShopPanel::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_pageshop($id) {
        $model = ShopPage::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_Cityshop($id) {
        $model = ShopCity::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_Menushop($id) {
        $model = ShopMenu::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_slidershop($id) {
        $model = ShopSlider::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_productshop($id) {
        $model = ShopProduct::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

   

    public function loadModel_shop($id) {
        $model = Shop::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

   
    public function loadModel_shopCategory($id) {
        $model = ShopNavbar::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

}
